package com.mondo.financialwallet.presentation.ui.fragment;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;

import com.example.common.ui.AbsNetworkStateFragment;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.FindEntity;
import com.mondo.financialwallet.presentation.model.DAppInfoModel;
import com.mondo.financialwallet.presentation.ui.adapter.SimpleDAppAdapter;
import com.mondo.financialwallet.presentation.ui.adapter.SimpleFragmentPagerAdapter;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.GlideImageLoader;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;

/**
 * 发现
 */
public class FindFragment extends AbsNetworkStateFragment {

    public static Fragment newInstance() {
        return new FindFragment();
    }

    @Bind(R.id.banner_view) Banner mBannerView;
    @Bind(R.id.hot_app_list) RecyclerView mHotAppList;
    @Bind(R.id.recently_used_list) RecyclerView mRecentlyUsedList;
    @Bind(R.id.app_list_view_pager) ViewPager mAppListViewPager;

    @Override
    protected void setStatusBar(Activity activity) {
        ActivityTitleBarHelper.setTransparentStatus(activity);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initActions() {
        mBannerView.setImageLoader(new GlideImageLoader());

        requestAppList();
    }

    private void requestAppList(){
        loadingNetwork();

        DataRepository.get().findDAppList(result -> {
            if (result.isSuccess()) {
                successNetwork();
                FindEntity.DataBean data = result.getData();

                // 广告
                initBanner(data.getBanner());

                // 最近使用
                List<FindEntity.DataBean.RecentlyBean> recently = data.getRecently();
                mRecentlyUsedList.setAdapter(SimpleDAppAdapter.createRecently(recently));

                // 热门应用
                List<FindEntity.DataBean.HeatDappsBean> hotApps = data.getHeat_dapps();
                mHotAppList.setAdapter(SimpleDAppAdapter.createHot(hotApps));

                initDAppList(data);

            } else {
                errorNetwork(result.getMsg());
            }
        });
    }

    private void initDAppList(FindEntity.DataBean data) {
        // 列表：热门
        ArrayList<DAppInfoModel> hotAppList = new ArrayList<>();
        List<FindEntity.DataBean.HeatBean> heats = data.getHeat();
        for (FindEntity.DataBean.HeatBean app : heats) {
            hotAppList.add(new DAppInfoModel(app.getName(), app.getLabel(), app.getIcon_url()));
        }

        // 列表：娱乐
        ArrayList<DAppInfoModel> entertainmentList = new ArrayList<>();
        List<FindEntity.DataBean.RecreationBean> recreation = data.getRecreation();
        for (FindEntity.DataBean.RecreationBean app : recreation) {
            entertainmentList.add(new DAppInfoModel(app.getName(), app.getLabel(), app.getIcon_url()));
        }

        // 列表：交易
        ArrayList<DAppInfoModel> transactionList = new ArrayList<>();
        List<FindEntity.DataBean.TransactionBean> transaction = data.getTransaction();
        for (FindEntity.DataBean.TransactionBean app : transaction) {
            transactionList.add(new DAppInfoModel(app.getName(), app.getLabel(), app.getIcon_url()));
        }

        // 列表：工具
        ArrayList<DAppInfoModel> toolsList = new ArrayList<>();
        List<FindEntity.DataBean.ToolBean> tool = data.getTool();
        for (FindEntity.DataBean.ToolBean app : tool) {
            toolsList.add(new DAppInfoModel(app.getName(), app.getLabel(), app.getIcon_url()));
        }

        // 列表：其他
        ArrayList<DAppInfoModel> otherList = new ArrayList<>();
        List<FindEntity.DataBean.OtherBean> other = data.getOther();
        for (FindEntity.DataBean.OtherBean app : other) {
            otherList.add(new DAppInfoModel(app.getName(), app.getLabel(), app.getIcon_url()));
        }

        // 列表
        mAppListViewPager.setAdapter(new SimpleFragmentPagerAdapter(
                getFragmentManager(),
                Arrays.asList("热门", "娱乐", "交易", "工具", "其他"),
                new ArrayList<>(Arrays.asList(
                        DAppListFragment.create(hotAppList),
                        DAppListFragment.create(entertainmentList),
                        DAppListFragment.create(transactionList),
                        DAppListFragment.create(toolsList),
                        DAppListFragment.create(otherList)
                ))
        ));
    }

    private void initBanner(List<FindEntity.DataBean.BannerBean> bannerList){
        List<String> links = new ArrayList<>();
        for (FindEntity.DataBean.BannerBean banner : bannerList) {
            links.add(banner.getIcon_url());
        }

        mBannerView.setImages(links).start();
    }

    @Override
    public void onAnewRequest() {
        requestAppList();
    }
}
